package com.fedex.longitude.view;

import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;

import com.fedex.longitude.domain.Navigator;
import com.fedex.longitude.domain.NavigatorLeaf;
import com.fedex.longitude.domain.NavigatorNode;

public class NavigationView extends ViewPart {

	public static final String ID = "com.fedex.longitude.view.NavigationView";

	private TreeViewer _treeViewer;

	private class NavigationViewProvider implements IStructuredContentProvider,
			ITreeContentProvider {

		@Override
		public void dispose() {
		}

		@Override
		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
		}

		@Override
		public Object[] getChildren(Object parent) {
			if (parent instanceof NavigatorNode) {
				return ((NavigatorNode) parent).getNavigators().toArray();
			}
			return new Object[0];
		}

		@Override
		public Object getParent(Object child) {
			if (child instanceof NavigatorLeaf) {
				return ((NavigatorLeaf) child).getParent();
			}
			return null;
		}

		@Override
		public boolean hasChildren(Object element) {
			if (element instanceof NavigatorNode) {
				return ((NavigatorNode) element).hasNavigators();
			}
			return false;
		}

		@Override
		public Object[] getElements(Object element) {
			return getChildren(element);
		}
	}

	private class NavigationLabelProvider extends LabelProvider {

		public String getText(Object obj) {
			if (obj instanceof Navigator) {
				return ((Navigator) obj).getName();
			}
			return null;
		}

		public Image getImage(Object obj) {
			if (obj instanceof NavigatorNode) {
				return PlatformUI.getWorkbench().getSharedImages()
						.getImage(ISharedImages.IMG_DEF_VIEW);
			} else if (obj instanceof NavigatorLeaf) {
				return PlatformUI.getWorkbench().getSharedImages()
						.getImage(ISharedImages.IMG_OBJ_FILE);
			}
			return null;
		}
	}

	@Override
	public void createPartControl(Composite parent) {
		NavigatorNode quickView = new NavigatorNode("Quick View");
		quickView.addNavigator(new NavigatorLeaf(quickView, "Dashboard"));
		
		NavigatorNode reports = new NavigatorNode("Reports");
		reports.addNavigator(new NavigatorLeaf(reports, "Product"));
		reports.addNavigator(new NavigatorLeaf(reports, "Scenario"));

		NavigatorNode analysis = new NavigatorNode("Analysis");
		analysis.addNavigator(new NavigatorLeaf(analysis, "Volume"));
		analysis.addNavigator(new NavigatorLeaf(analysis, "Change"));
		analysis.addNavigator(new NavigatorLeaf(analysis, "Versus"));

		NavigatorNode root = new NavigatorNode("");
		root.addNavigator(quickView);
		root.addNavigator(reports);
		root.addNavigator(analysis);
		
		_treeViewer = new TreeViewer(parent, SWT.H_SCROLL | SWT.V_SCROLL);
		_treeViewer.getControl().setBackground(new Color(null, new RGB(220, 220, 220)));
		_treeViewer.setContentProvider(new NavigationViewProvider());
		_treeViewer.setLabelProvider(new NavigationLabelProvider());
		_treeViewer.setInput(root);
		_treeViewer.setExpandedElements(new Object[]{quickView});
	}

	@Override
	public void setFocus() {
		_treeViewer.getControl().setFocus();
	}
}
